package com.king.zxing.app.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.king.zxing.app.R;
import com.king.zxing.app.domain.Goods;
import com.king.zxing.app.utils.DBSqlLiteHelper;

import java.util.ArrayList;
import java.util.List;

public class GoodsDao {
    DBSqlLiteHelper myHelper;
    SQLiteDatabase db;

    static private final String TABLE_NAME = "Goods";

    public GoodsDao(Context context) {
        this.myHelper = new DBSqlLiteHelper(context);
        try {
            this.db = this.myHelper.getWritableDatabase();
        } catch (Exception e) {
            this.db = this.myHelper.getReadableDatabase();
        }

        String sql = "CREATE TABLE IF NOT EXISTS Goods (  \n" +
                "    ID INTEGER PRIMARY KEY AUTOINCREMENT,   -- 商品ID（系统生成）  \n" +
                "    Name TEXT NOT NULL,                     -- 商品名  \n" +
                "    Price TEXT NOT NULL,                    -- 价格\n" +
                "    ImageID INTEGER                         -- 图片\n" +
                ");";

        try {
            this.db.execSQL(sql);
        } catch (Exception e) {
            Log.e("goods表创建异常", e.toString());
        }


        if (getGoodsList().isEmpty()) {
            Log.e("cloyir", "GoodsList初始化");
            long ignored1 = addGoods("Relea咖啡杯", "140", 1);
            long ignored2 = addGoods("SOLEHE控油祛痘沐浴露", "280", 2);
            long ignored3 = addGoods("端午礼盒香薰", "159", 0);
            long ignored4 = addGoods("东方苏打 长生之脉", "90", 3);
        }

    }

    static private final String[] Args = new String[]{
            "ID", "Name", "Price", "ImageID"
    };

    public long addGoods(String Name, String Price, long ImageID) {
        if (db.isReadOnly()) return -1;
        ContentValues values = new ContentValues();
        values.put("Name", Name);
        values.put("Price", Price);
        values.put("ImageID", ImageID);
        return this.db.insert(TABLE_NAME, null, values);
    }

    public List<Goods> getGoodsList() {
        List<Goods> ans = new ArrayList<>();
        Cursor cursor = db.query(TABLE_NAME, Args, null, null, null, null, null);

        while (cursor.moveToNext()) {
            List<Integer> ptr = new ArrayList<>();
            for (String item : Args) ptr.add(cursor.getColumnIndex(item));
            long ID = cursor.getLong(ptr.get(0));
            String Name = cursor.getString(ptr.get(1));
            String Price = cursor.getString(ptr.get(2));
            long ImageID = cursor.getLong(ptr.get(3));
            ans.add(new Goods(ID, Name, Price, ImageID, new ArrayList<>()));
        }
        cursor.close();
        return ans;
    }

    public Goods getGoodsByID(long id) {
        Cursor cursor = db.query(TABLE_NAME, Args, "ID=?", new String[]{String.valueOf(id)}, null, null, null);

        if (cursor.moveToNext()) {
            List<Integer> ptr = new ArrayList<>();
            for (String item : Args) ptr.add(cursor.getColumnIndex(item));
            long ID = cursor.getLong(ptr.get(0));
            String Name = cursor.getString(ptr.get(1));
            String Price = cursor.getString(ptr.get(2));
            long ImageID = cursor.getLong(ptr.get(3));

            cursor.close();
            return new Goods(ID, Name, Price, ImageID, new ArrayList<>());
        }
        cursor.close();
        return null;
    }
}
